﻿<?php
/**
 * commandPayTradeCreatePay	指令到帐支付跳转	接口
 * Date: 2016/11/24 14:34
 */

function testResponse(){
    $config = $GLOBALS['config'];
    $user = $GLOBALS['user'];

    //创建请求对象
    $objReq = new \yijipay\message\request\CommandPayTradeCreatePayRequest();

    //请求公共部分
    $objReq->setReturnUrl(getReturnUrl());
    $objReq->setNotifyUrl(getNotifyUrl());
    //partnerId默认yijipay\config.php中已经配置
    //$objReq->setPartnerId($config['partnerId']);
    $objReq->setMerchOrderNo("T" . 'yuihjkuioyuihjkuioyuihjkuio');
    $objReq->setOrderNo("RID".genOrderNo());
    //signType默认MD5
    $objReq->setSignType("RSA");
    //$objReq->setContext("");

    //构建交易参数
    $objReq->setBuyerOrgName("买家PHP");
    $objReq->setBuyerRealName("买家PHP");
    $objReq->setBuyerUserId($user['buyer_uid_yiji']);
    $objReq->setOutUserId($user['buyer_id']);

    $item1 = genCommandPayOrder($user);
    $objReq->setCommandPayOrders([$item1]);

    //收银台参数
    //$objReq->setPaymentType("QUICKPAY");
//    $objReq->setPaymentType("ONLINEBANK");
    $objReq->setUserTerminalType("PC");
    $objReq->setBuyerOrgName("买家公司名");

    echo "================request: preSignStr：";
    echo "<br/>". \yijipay\SignHelper::getPreSignStr($objReq);

    //构建请求
    $client = new \yijipay\YijiPayClient($config);


    //方式1：重定向到易极付页面
 //   $html = $client->pageExecute($objReq);
 //   echo $html;
 //   exit;

   //方式2: 获取重定向URL
    $response = $client->execute($objReq);
    $resp = json_decode($response);
    if($client->verify($response) && $resp->success){
        //================================
        //此处填写创建成功的代码
        //================================
    }else{
		//================================
        // 此处填写创建成功的代码
        //================================
        throw new Exception($resp->resultMessage);
    }

    exit;

}

/**
 * 构建商品订单
 * @param $user
 * @return \yijipay\message\request\dto\CommandPayOrder
 */
function genCommandPayOrder($user){
    $order = new \yijipay\message\request\dto\CommandPayOrder();

    $order->setMerchOrderNo("MON". genOrderNo());
    $order->setTradeName("购买电脑");
    $order->setSellerUserId($user['seller_uid_yiji']);
    $order->setPactNo(null);
    $order->setTradeAmount("84.51");

    $order->setGoodsTypeCode("5146854");
    $order->setGoodsTypeName("计算机");
    $order->setGoodsName("笔记本电脑");

    $order->setMemo("备注");
    $order->setSellerOrgName("猪八戒科技有限公司");
    $order->setAutoCloseDuration("14400");

    return $order;
}